(function($, doc) {
	var settings = app.getSettings();
	var state = app.getState();
	//app.cklogin(state.user_id);
	//console.log(state.phone);
	//console.log(state.user_id);
	//console.log(settings);
	window.addEventListener('show', function() {
		//console.log(state.user_id);
	}, false);
	var pages = ["chat.html", "contact.html", "find.html", "me.html"];
	var subStyle = {
		top: '45px',
		bottom: '50px'
	};
	$.init({
		swipeBack: false,
		beforeback: back
	});
	//创建窗体对象，把子窗口依次放入
	$.plusReady(function() {
		//1.获取到当前显示窗口
		var obj = plus.webview.currentWebview();
		
		for(var i = 0; i < pages.length; i++) {
			//创建多个窗口
			var sub = plus.webview.create(pages[i], pages[i], subStyle);
			sub.hide(); //隐藏窗口
			//加入到当前窗口，成为子窗口
			obj.append(sub);
		}
		//plus.webview.create(pages[0], pages[0], subStyle);
		plus.webview.show(pages[0]);

	});
	//选项卡点击事件
	var activeTab = pages[0];
	$('.mui-bar-tab').on('tap', 'a', function(e) {
		var targetTab = this.getAttribute('href');
		if(targetTab != activeTab) {
			//隐藏当前;
			plus.webview.hide(activeTab);
			//显示目标选项卡
			plus.webview.show(targetTab, "slide-in-right", 300);
			//plus.webview.close(activeTab);
			//plus.webview.create(targetTab, targetTab, subStyle);
			//plus.webview.show(targetTab);
			title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;
			//更改当前活跃的选项卡
			activeTab = targetTab;
		}

	});
	var main, menu, mask = mui.createMask(_closeMenu),
		showMenu = false;

	function back() {
		if(showMenu) {
			//菜单处于显示状态，返回键应该先关闭菜单,阻止主窗口执行mui.back逻辑；
			closeMenu();
			return false;
		} else {
			//菜单处于隐藏状态，执行返回时，要先close菜单页面，然后继续执行mui.back逻辑关闭主窗口；
			menu.close('none');
			return true;
		}
	}
	$.plusReady(function() {
		main = plus.webview.currentWebview();
		//setTimeout的目的是等待窗体动画结束后，再执行create webview操作，避免资源竞争，导致窗口动画不流畅；
		setTimeout(function() {
			//侧滑菜单默认隐藏，这样可以节省内存；
			menu = $.preload({
				id: 'plus-menu',
				url: 'pages/public/menu.html',
				styles: {
					top: '45px',
					right: '10px',
					width: '200px',
					height: '270px',
					zindex: 9997
				}
			});
		}, 300);

	});
	/**
	 * 显示菜单菜单
	 */
	function openMenu() {
		//console.log(showMenu);
		if(!showMenu) {
			//侧滑菜单处于隐藏状态，则立即显示出来；
			//显示完毕后，根据不同动画效果移动窗体；
			menu.show();
			//显示遮罩
			mask.show();
			showMenu = true;
		}
	}

	function ocMenu() {
		if(!showMenu) {
			openMenu();
		} else {
			closeMenu();
		}
	}
	/**
	 * 关闭侧滑菜单
	 */
	function closeMenu() {
		_closeMenu();
		//关闭遮罩
		mask.close();
	}
	/**
	 * 关闭侧滑菜单（业务部分）
	 */
	function _closeMenu() {
		if(showMenu) {
			//关闭遮罩；
			main.setStyle({
				top: '0',
				transition: {
					duration: 150
				}
			});

			//等窗体动画结束后，隐藏菜单webview，节省资源；
			setTimeout(function() {
				menu.hide();
			}, 200);
			//改变标志位
			showMenu = false;
		}
	}

	document.querySelector('#main_popover').addEventListener('tap', ocMenu);
	$.menu = function() {
		if(showMenu) {
			closeMenu();
		} else {
			openMenu();
		}
	}
	//plus.webview.show(pages[0]);
	doc.querySelector('#mainsearchbtn').addEventListener('tap', function() {
		$.openWindow({
			url: 'pages/public/search.html',
			id: 'pages/public/search.html',
			show: {
				autoShow: true,
				aniShow: 'slide-in-right',
				duration: 100
			},
			waiting: {
				autoShow: true,
				title: 'waiting...'
			},
			extras: {}
		});
	});
	$.plusReady(function() {
		var server = apiurl + "index/update"; //获取升级描述文件服务器地址
		mui.getJSON(server, {
			"appid": plus.runtime.appid,
			"version": plus.runtime.version,
			"imei": plus.device.imei
		}, function(data) {
			if(data.status) {
				plus.ui.confirm(data.note, function(i) {
					if(0 == i) {
						plus.runtime.openURL(data.url);
					}
				}, data.title, ["立即更新", "取　　消"]);
			}
		});
	});
	window.addEventListener('closeMenu', function(e) {
		closeMenu();
	});
	//var uid = state.user_id;
	/*
	if(uid) {
		var chatdata = localforage.getItem('chatdata' + uid);
		//var chatlist = localforage.getItem('chatlist' + uid);
		var last_time = localStorage.getItem('last_time' + uid) || [];
		var chatlist = localforage.getItem('chatlist' + uid) || [];
		var indexlist = localforage.getItem('indexlist' + uid) || [];
		//console.log(last_time);
		var list_time = 0;
		var postparam = {
			"user_id": uid,
			"last_time": last_time
		};
		var server = apiurl + 'chat/getlist';
		var getwebdata = function() {
			//console.log(chatdata.list.length);
			mui.getJSON(server, postparam, function(data) {
				if(data.status < 1) {
					return;
				}
				console.log(data.status);
				console.log(data.last_time);
				if(data.last_time) {
					//chatdata.last_time = data.last_time;
					var clist = chatdata.list || [], //所有聊天数据
						dlist = data.list; //服务器数据
					//console.log(chatdata.last_time);
					//console.log(dlist.length);
					if(dlist) {
						for(var i = 0; i < dlist.length; i++) {
							clist[dlist[i]['m_id']] = dlist[i];
						}
					}else{
						return;
					}

					localStorage.setItem('last_time' + uid, data.last_time);
					//chatdata.list = clist;
					localforage.setItem('chatdata' + uid, clist);

					
					//联系人列表
					for(var p in clist) {
						var cdata = clist[p];
						var meid = cdata['f_id'];
						var toid = cdata['t_id'];
						if(toid == uid) {//发给我的信息
							chatlist[toid].push(cdata);
						}
						
					}
					//我发的信息
					for(var toid in chatlist) {					
						for(var j in clist) {
							if(clist[j]['t_id'] == toid && clist[j]['f_id'] == uid) {
								chatlist[toid].push(clist[j]);
							}
						}
					}
					//按时间排序
					for(var p in chatlist) {
						chatlist[p] = app.arrSort(chatlist[p], 'add_time');
					}
					localforage.setItem('chatlist' + uid, chatlist);
					for(var p in chatlist) {
						//indexlist[p] = chatlist[p].slice(0, 1);
						var one = chatlist[p].shift();
						var pushdata = {
							user_id: one['t_id'],
							name: one['t_name'],
							avatar: one['t_head'],
							lastmsg: one['t_msg'],
							'ltime': one['add_time'],
							//istop: 0
						};
						pushdata.istop = indexlist[p]['istop'] ? indexlist[o]['istop'] : 0;
						indexlist[p].push(pushdata);
					}
					localforage.setItem('indexlist' + uid, indexlist);
				}
			});
		};
		getwebdata();
		//console.log(chatdata);
		// getsys = setInterval(getwebdata,5000);

	}*/
}(mui, document));

//var userinfo = get_data('user_info');
//var userinfo = localforage.getItem('userinfo', function(err, val) {
//	//console.log(err);
//	console.log(value);
//});

//console.log(chatdata);
/*
var chatdata = localforage.getItem('chatdata');
if(userinfo.user_id) {
	console.log(userinfo.user_id);
	var postparam = {
		"user_id": userinfo.user_id,
		"last_time": chatdata.last_time
	};
	//var chatdata = rt.get('chatdata');
	//var getsys = setInterval(getwebdata,2000);
	var server = apiurl + 'chat/getlist';

	if(chatdata) {
		var postparam = {
			"user_id": userinfo.user_id,
			"last_time": chatdata.last_time
		};
		//var getsys = setInterval(getwebdata, 2000);
		function getwebdata() {
			console.log(chatdata.list.length);
			mui.getJSON(server, postparam, function(data) {
				console.log(data.last_time);
				if(data.last_time) {
					chatdata.last_time = data.last_time;
					var clist = chatdata.list || [],
						dlist = data.list;
					//console.log(chatdata.last_time);
					//console.log(dlist.length);
					for(var i = 0; i < dlist.length; i++) {
						clist[dlist[i]['m_id']] = dllist[i];
					}
					//chatdata.list = clist;
					//localforage.setItem('chatdata', chatdata);
				}
			});
		}
	}
}
*/